const resolvePath = (path: string) => {
  if (path.startsWith('@/')) return path.replace('@/', '')
  if (path.startsWith('/')) return path.replace('/', '')
  else return path
}
/**
 * 深拷贝
 * @param data
 */
const deepClone = <T>(data: T): T => {
  if (!data) {
    return data
  }
  if (typeof data !== 'object') {
    return data
  }
  if (Array.isArray(data)) {
    return deepArray(data)
  }
  return deepObject(data as ObjectStringIndex<Object>)
}
/**
 * 深拷贝对象
 * @param object
 */

const deepObject = <T>(object: ObjectStringIndex<Object>): T => {
  const keys = Object.keys(object)
  const result: ObjectStringIndex<Object> = {}
  keys.forEach(item => {
    result[item] = deepClone(object[item] as ObjectStringIndex<Object>)
  })
  return result as T
}
/**
 * 深拷贝数组
 * @param arr
 */
const deepArray = <T extends Array<any>>(arr: T): T => {
  const result: Array<any> = arr.map(deepClone)
  return result as T
}

/**
 * 导入
 * **/
export const getFileUrl = (url: string): string => {
  return new URL(`../${resolvePath(url)}`, import.meta.url).href
}

export const getTreeData = (list: Array<any>) => {
  const obj: any = {}
  list.forEach((item: any) => {
    obj[item.id] = item
  })
  list.forEach((ele: any) => {
    if (obj[ele.pId]) {
      obj[ele.pId].children ? obj[ele.pId].children.push(ele) : (obj[ele.pId].children = [ele])
    }
  })
}

export const getDate = (param: Date): string => {
  let year: number | string = param.getFullYear()
  let month: number | string = param.getMonth() + 1
  let date: number | string = param.getDate()
  let result: string = ''
  if (date >= 1 && date <= 9) {
    //日如果小于10就补个0
    date = '0' + date
  }
  if (month >= 1 && month <= 9) {
    //月如果小于10就补个0
    month = '0' + month
  }
  result = year + '-' + month + '-' + date
  return result
}
